package com.caseProcess.controller;

import com.caseProcess.entity.Department;
import com.caseProcess.entity.Result;
import com.caseProcess.service.DepartmentService;
import com.caseProcess.utils.PagingUtil;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author ahuo
 * @version 1.0
 * @date 2020年11月15日 13点15分
 */
@RestController
@RequestMapping("/dep")
public class DepartmentController {

    @Resource
    DepartmentService departmentService;

    /*获取部门信息*/
    @RequestMapping("/getDepartment")
    public Result getDepartment(Long deepth,@RequestParam(required = false,defaultValue = "0")int page,
                                @RequestParam(required = false,defaultValue = "0")int limit){
        List<Department> departmentList = departmentService.getDepartments(deepth);
        Result result = new Result();
        result.setCount(departmentList.size());
        if (departmentList.size() >= 0){
            if(page!=0&&limit!=0){
                departmentList= PagingUtil.paging(departmentList,page,limit);
            }
            result.setCode(Result.RESULT_OK);
            result.setMsg("操作成功");
            result.setData(departmentList);
        }else{
            result.setCode(Result.RESULT_ERROR);
            result.setMsg("操作失败");
        }

        return result;
    }

    /**
     * 条件查询部门
     * @param departmentId
     * @param departmentName
     * @param description
     * @return
     */
    @RequestMapping("/getDepByCondition")
    public Result getDepByCondition(String departmentId,String departmentName,String description){
        System.out.println(departmentId+departmentName+description);
        Result result = new Result();
        try {
            List<Department> departmentList = departmentService.getDepByConditions(departmentId,departmentName,description);
        if (departmentList.size() >= 0){
            result.setCode(Result.RESULT_OK);
            result.setMsg("操作成功");
            result.setData(departmentList);
        }else{
            result.setCode(Result.RESULT_ERROR);
            result.setMsg("操作失败");
        }
        }catch (Exception e){
            e.printStackTrace();
        }
        return result;
    }

    /*查看部门信息*/
    @RequestMapping("/select")
    public Result selectDe(){
        Result result = new Result();
        List<Department> departments = departmentService.selectDepartments();
        if (departments.size() >= 0){
            result.setCode(Result.RESULT_OK);
            result.setMsg("部门信息查询成功");
            result.setCount(departments.size());
            result.setData(departments);
        }else{
            result.setCode(Result.RESULT_ERROR);
            result.setMsg("服务器错误");
        }
        return result;

    }

    /*添加部门信息*/
    @RequestMapping("/insert")
    public Result insertDep(String departmentId,String departmentName,String description,Long deepth) {
        Result result = new Result();
        Department depat=departmentService.isExist(departmentId,departmentName);
        if (depat!=null){
            result.setCode(Result.RESULT_ERROR);
            result.setMsg("部门信息添加失败");
            return result;
        }else {
            Department department = new Department();
            department.setDepartmentId(departmentId);
            department.setDepartmentName(departmentName);
            department.setDescription(description);
            department.setDeepth(deepth);
            departmentService.insert(department);
            result.setCode(Result.RESULT_OK);
            result.setMsg("部门信息添加成功");

            result.setData(department);
            return result;
        }
    }

    /*更新部门信息*/
    @RequestMapping("update")
    public Result updateDep(String departmentId,String departmentName,String description){
        Department department=new Department();
        department.setDepartmentId(departmentId);
        department.setDepartmentName(departmentName);
        department.setDescription(description);
        departmentService.update(department);
        Result result = new Result();
        result.setCode(Result.RESULT_OK);
        result.setMsg("部门信息添加成功");
        result.setData(department);
        return result;
    }

    /*删除部门信息*/
    @RequestMapping("delete")
    public Result deleteDep(String departmentId){
       Department department=new Department();
       department.setDepartmentId(departmentId);
        int a=departmentService.delete(departmentId);
        Result result = new Result();
        result.setCode(Result.RESULT_OK);
        result.setMsg("部门信息删除成功");
        result.setData(a);
        return result;
    }
}
